/**
 * 
 * api授权访问控制
 */
module.exports = warrantRule => (req, resp, next) => {
    let warrantConfig = require('../../config/apiwarrant');
    let warrantLicences = warrantConfig.licences;
    if (req.method == 'OPTIONS') {
        console.log(`pass the options request for ${req.url}`);
        next();
        return;
    }
    let apiKey = req.headers['warrantwho'] || req.headers['WarrantWho'];
    if (!apiKey) {
        throw {
            code: 403,
            message: 'do you know who is Warrant?',
        }
    }
    let targetItem = warrantLicences[warrantRule.licenceName].find(item => item.api_key == apiKey);
    if (!targetItem) {
        throw {
            code: 403,
            message: 'your Warrant is invalid?',
        }
    }
    next();
}